#ifndef LOGMANAGER_H
#define LOGMANAGER_H

#include <QObject>
#include <QFile>
#include <QTextStream>
#include <QDateTime>
#include <QDir>

class LogManager : public QObject
{
    Q_OBJECT

public:
    // 获取单例实例
    static LogManager* instance();

    // 删除拷贝构造函数和赋值操作符，确保单例唯一性
    LogManager(const LogManager&) = delete;
    LogManager& operator=(const LogManager&) = delete;

    // 写入日志
    void writeLog(const QString &message, const QString &level = "INFO");

    // 设置日志文件路径
    void setLogPath(const QString &path);

private:
    explicit LogManager(QObject *parent = nullptr);
    ~LogManager();

    QFile *logFile;
    QTextStream *logStream;
    QString logPath;

    // 获取当前日期字符串
    QString getCurrentDate() const;

    // 打开日志文件
    bool openLogFile();

    // 关闭日志文件
    void closeLogFile();

    // 单例实例指针
    static LogManager* m_instance;
};

// 全局日志宏定义，方便使用
#define LOG_INFO(msg) LogManager::instance()->writeLog(msg, "INFO")
#define LOG_WARNING(msg) LogManager::instance()->writeLog(msg, "WARNING")
#define LOG_ERROR(msg) LogManager::instance()->writeLog(msg, "ERROR")

#endif // LOGMANAGER_H
